package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorService;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dto.LikesBehaviorDto;
import com.heima.model.behavior.dto.ReadDto;
import com.heima.model.behavior.dto.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.common.ApuserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApBehaviorServiceImpl implements ApBehaviorService {
    @Autowired
    private CacheService cacheService;
    /**
     * 用户点赞功能
     * @param dto
     * @return
     */
    @Override
    public ResponseResult likeBehavior(LikesBehaviorDto dto) {

        //1.参数检查
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.从线程中去获取用户信息   获取当前用户登录的id
        ApUser apUser = ApuserThreadLocalUtil.get();
        if (apUser ==null && dto.getEquipmentId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //3.获取用户的id
        Integer apUserId = null;
        if (apUser != null){
            apUserId = apUser.getId();
            if (apUserId == 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            }
        }

        //4.将数据存储到redis中
        //定义一个唯一的key
        String key = "LIKE_" + apUserId;

        Map<String, String> map = new HashMap<>();
        map.put("articleId",dto.getArticleId() + "");
        map.put("type",dto.getType()+"");
        map.put("operation",dto.getOperation() + "");
        map.put("equipmentId",dto.getEquipmentId() + "");

        //5.保存
        cacheService.hPut(key,dto.getArticleId() + "", JSON.toJSONString(map));
        //cacheService.hPutAll(key,map);

        //保存结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 喜欢不喜欢功能
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikeBehavior(UnLikesBehaviorDto dto) {
        //1.参数检查
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.从线程中去获取用户信息   获取当前用户登录的id
        ApUser apUser = ApuserThreadLocalUtil.get();
        if (apUser ==null && dto.getEquipmentId()  == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //3.获取用户的id
        Integer apUserId = null;
        if (apUser != null){
            apUserId = apUser.getId();
            if (apUserId == 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            }
        }

        //4.将数据存储到redis中
        //定义一个唯一的key
        String key = "UNLIKE_" + apUserId;
        Map<String, String> map = new HashMap<>();
        map.put("articleId",dto.getArticleId() + "");
        map.put("type",dto.getType()+"");
        map.put("equipmentId",dto.getEquipmentId() + "");

        //5.保存
        cacheService.hPut(key,dto.getArticleId() + "", JSON.toJSONString(map));
       // cacheService.hPutAll(key,map);
        //保存结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 统计阅读次数
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(ReadDto dto) {
        //1.参数检查
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.从线程中去获取用户信息   获取当前用户登录的id
        ApUser apUser = ApuserThreadLocalUtil.get();
        if (apUser ==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //3.获取用户的id
        Integer apUserId = null;
        if (apUser != null){
            apUserId = apUser.getId();
            if (apUserId == 0){
                return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
            }
        }
        //4.将数据存储到redis中
        //定义一个唯一的key
        String key = "READ_"  + apUserId;
        Map<String, String> map = new HashMap<>();
        map.put("articleId",dto.getArticleId() + "");

        //判断redis是否存在count字段
        boolean result = cacheService.hExists(key, "count");
        Integer count = dto.getCount();
        if (!result){
            //如果不存在
            map.put("count",count + "");
        }else {
            //数量加1
            Long count1 = cacheService.hIncrBy(key, "count", 1);
            map.put("count", String.valueOf(count1));
        }

        //cacheService.hPut(key,dto.getArticleId() + "", JSON.toJSONString(map));
        cacheService.hPutAll(key,map);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
